package com.axend.aerosense.base.viewmodel;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.bean.BaseBean;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;

import java.lang.reflect.InvocationTargetException;

public class BaseViewModelFactroy<T extends BaseBean> implements ViewModelProvider.Factory {
    IBaseView iBaseView;
    ILoadDataResult<T> iLoadDataResult;

    public BaseViewModelFactroy(IBaseView baseView, ILoadDataResult<T> loadDataResult) {
        iBaseView = baseView;
        iLoadDataResult = loadDataResult;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        try {
            return modelClass.getConstructor(IBaseView.class, ILoadDataResult.class).newInstance(iBaseView, iLoadDataResult);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
